package com.guigu.www.tangyingmeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import com.guigu.www.tangyingmeng.service.TymWrokService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class TymWrokServiceImpl implements TymWrokService {

    @Autowired
    private InterventionMapper interventionMapper;
    @Autowired
    private HealthManagerMapper healthManagerMapper;
    @Autowired
    private BespeakResultMapper resultMapper;
    @Autowired
    private FoodZongMapper foodZongMapper;
    @Autowired
    private KeepZongMapper keepZongMapper;
    @Autowired
    private TemplateWholesomeMapper wholesomeMapper;
    @Autowired
    private InterventionTemplateMapper templateMapper;

    @Override
    public PageVo<Intervention> selworkall(Map<String, String> map) {
        QueryWrapper<Intervention> wrapper = new QueryWrapper<>();
        wrapper.like("inter_id",map.get("findid")).like("inter_execute_person", map.get("findname"));
        IPage<Intervention> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = interventionMapper.selectPage(page, wrapper);
        PageVo<Intervention> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        for (Intervention result : pageVo.getRows()) {
            result.setHealthManager(healthManagerMapper.selectById(result.getHmId()));
        }
        return pageVo;
    }

    @Override
    public Boolean statemethod(Integer id) {
        Intervention intervention = new Intervention();
        intervention.setInterId(id);
        intervention.setInterState(1);
        int update = interventionMapper.updateById(intervention);
        if(update>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean stopmethod(Integer id) {
        Intervention intervention = new Intervention();
        intervention.setInterId(id);
        intervention.setInterState(0);
        int update = interventionMapper.updateById(intervention);
        if(update>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean delwork(Integer id) {
        int delete = interventionMapper.deleteById(id);
        if(delete>0){
            QueryWrapper<BespeakResult> wrapper = new QueryWrapper<>();
            wrapper.eq("inter_id",id);
            BespeakResult result = resultMapper.selectOne(wrapper);
            int update = resultMapper.updateByInterId(result.getBesResultId());
            if(update>0){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public Intervention selworkone(Integer id) {
        Intervention intervention = interventionMapper.selectById(id);
        return intervention;
    }

    //查询所有干预方案模板
    @Override
    public List<InterventionTemplate> seltemplateall() {
        QueryWrapper<InterventionTemplate> wrapper = new QueryWrapper<>();
        wrapper.eq("inter_temp_state",0);
        List<InterventionTemplate> list = templateMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<TemplateWholesome> selwhoall() {
        QueryWrapper<TemplateWholesome> wrapper = new QueryWrapper<>();
        wrapper.eq("temp_who_state",0);
        List<TemplateWholesome> list = wholesomeMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<KeepZong> selkeepall() {
        QueryWrapper<KeepZong> wrapper = new QueryWrapper<>();
        wrapper.eq("keep_zong_state",0);
        List<KeepZong> list = keepZongMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<FoodZong> selfoodall() {
        QueryWrapper<FoodZong> wrapper = new QueryWrapper<>();
        wrapper.eq("food_zong_state",0);
        List<FoodZong> list = foodZongMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Boolean upwork(Intervention intervention) {
        if (intervention.getTemid() != null) {
            InterventionTemplate template = templateMapper.selectById(intervention.getTemid());
            intervention.setTempFoodId(template.getTempFoodId());
            intervention.setTempKeepId(template.getTempKeepId());
            intervention.setTempWhoId(template.getTempWhoId());
        }
        int update = interventionMapper.updateById(intervention);
        if(update>0){
            return true;
        }else {
            return false;
        }
    }

}
